
const config = Symbol('config')
const isCompleteURL = Symbol('isCompleteURL')
const requestBefore = Symbol('requestBefore')
const requestAfter = Symbol('requestAfter')
class MinRequest {
	// 默认配置
	[config] = {
		baseURL: '',
		header: {
			'content-type': 'application/json', // application/json || application/x-www-form-urlencoded
		},
		method: 'GET',
		timeout: 60000,
		dataType: 'json',
		responseType: 'text',
		withCredentials: true
	}
	// 拦截器
	interceptors = {
		// 请求前拦截处理
		request: (func) => {
			if (func) {
				MinRequest[requestBefore] = func
			} else {
				MinRequest[requestBefore] = (request) => request
			}
		},
		// 响应后拦截处理
		response: (func) => {
			if (func) {
				MinRequest[requestAfter] = func
			} else {
				MinRequest[requestAfter] = (response) => response
			}
		}
	}
	// 前
	static[requestBefore](config) {
		return config // 返回默认配置
	}
	// 后
	static[requestAfter](response) {
		return response
	}
	// 判断url是否完整
	static[isCompleteURL](url) {
		return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
	}
	// 设置配置
	setConfig(func) {
		this[config] = func(this[config])
	}
	// 请求
	async request(options = {}) {
		options.baseURL = options.baseURL || this[config].baseURL
		options.dataType = options.dataType || this[config].dataType
		options.responseType = options.responseType || this[config].responseType
		options.apiUrl = options.url
		options.url = MinRequest[isCompleteURL](options.url) ? options.url : (options.baseURL + options.url)
		options.data = options.data
		options.method = options.method || this[config].method
		options.timeout = options.timeout || this[config].timeout
		options.loading = [null, undefined, ''].includes(options.loading) ? true : options.loading // 加载交互
		options.header = Object.assign(options.header, this[config].header)
		// options.withCredentials = true
		Object.assign(options, await MinRequest[requestBefore](options))
		return new Promise((resolve, reject) => {
			// 成功
			options.success = (res)=> {
				res.config = options
				resolve(MinRequest[requestAfter](res))
			}
			// 失败
			options.fail = (err)=> {
				reject(MinRequest[requestAfter](err))
			}
			// 请求接口
			uni.request(options)
		})
	}
	// get请求
	get(url, data, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		options.header = {}
		return this.request(options)
	}
	// post请求
	post(url, data, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		options.header = {}
		return this.request(options)
	}
	// put请求
	put(url, data, options = {}) {
		options.url = url
		options.data = data
		options.method = 'PUT'
		options.header = {}
		return this.request(options)
	}
	// delete请求
	delete(url, data, options = {}) {
		options.url = url
		options.data = data
		options.method = 'DELETE'
		options.header = {}
		return this.request(options)
	}
}

MinRequest.install = function(Vue) {
	Vue.mixin({
		beforeCreate: function() {
			if (this.$options.minRequest ) {
				Vue._minRequest = this.$options.minRequest 
			}
		}
	})
	Object.defineProperty(Vue.prototype, '$api', {
		get: function() {
			return Vue._minRequest.apis
		}
	})
}

export default MinRequest
